{% extends "base.html" %}

{% block title %}传输历史 - 镜像传输系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header bg-info text-white d-flex justify-content-between align-items-center">
                <h4 class="mb-0"><i class="fas fa-history"></i> 传输历史</h4>
                <div>
                    <button class="btn btn-light btn-sm me-2" onclick="reloadConfig()">
                        <i class="fas fa-sync-alt"></i> 重载配置
                    </button>
                    <button class="btn btn-light btn-sm" onclick="refreshHistory()">
                        <i class="fas fa-redo"></i> 刷新
                    </button>
                </div>
            </div>
            <div class="card-body">
                {% if tasks %}
                <div class="table-responsive">
                    <table class="table table-hover table-striped">
                        <thead class="table-dark">
                            <tr>
                                <th>任务ID</th>
                                <th>镜像名称</th>
                                <th>目标服务器</th>
                                <th>目标路径</th>
                                <th>状态</th>
                                <th>进度</th>
                                <th>创建时间</th>
                                <th>完成时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for task in tasks %}
                            <tr>
                                <td>{{ task.id }}</td>
                                <td>
                                    <span class="badge bg-secondary">{{ task.image_name }}</span>
                                </td>
                                <td>{{ task.target_server.name if task.target_server else 'N/A' }}</td>
                                <td>
                                    <code>{{ task.target_path }}</code>
                                </td>
                                <td>
                                    {% if task.status == 'completed' %}
                                        <span class="badge bg-success">成功</span>
                                    {% elif task.status == 'failed' %}
                                        <span class="badge bg-danger">失败</span>
                                    {% elif task.status == 'pending' %}
                                        <span class="badge bg-secondary">等待中</span>
                                    {% elif task.status == 'pulling' %}
                                        <span class="badge bg-info">拉取中</span>
                                    {% elif task.status == 'saving' %}
                                        <span class="badge bg-warning">保存中</span>
                                    {% elif task.status == 'transferring' %}
                                        <span class="badge bg-primary">传输中</span>
                                    {% else %}
                                        <span class="badge bg-secondary">{{ task.status }}</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="progress" style="height: 20px; width: 100px;">
                                        <div class="progress-bar" style="width: {{ task.progress }}%">
                                            {{ task.progress }}%
                                        </div>
                                    </div>
                                </td>
                                <td>{{ task.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                                <td>
                                    {% if task.completed_at %}
                                        {{ task.completed_at.strftime('%Y-%m-%d %H:%M:%S') }}
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <button class="btn btn-sm btn-info" onclick="viewTaskDetails({{ task.id }})">
                                            <i class="fas fa-info-circle"></i> 详情
                                        </button>
                                        {% if task.status == 'failed' %}
                                        <button class="btn btn-sm btn-warning" onclick="retryTask({{ task.id }})">
                                            <i class="fas fa-redo"></i> 重试
                                        </button>
                                        {% endif %}
                                        <button class="btn btn-sm btn-danger" onclick="deleteTask({{ task.id }})">
                                            <i class="fas fa-trash"></i> 删除
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-inbox display-1 text-muted"></i>
                    <h5 class="text-muted">暂无传输历史</h5>
                    <p class="text-muted">开始您的第一个镜像传输任务吧！</p>
                    <a href="{{ url_for('transfer') }}" class="btn btn-primary">
                        <i class="fas fa-arrow-left-right"></i> 开始传输
                    </a>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 详情模态框 -->
<div class="modal fade" id="taskDetailsModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">任务详情</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <pre id="taskDetailsContent" class="bg-light p-3 rounded"></pre>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除这个传输任务吗？此操作不可撤销。</p>
                <p class="text-muted">任务ID: <span id="deleteTaskId"></span></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" onclick="confirmDeleteTask()">确认删除</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
let currentDeleteTaskId = null;

function viewTaskDetails(taskId) {
    fetch(`/api/tasks/${taskId}`)
        .then(response => response.json())
        .then(data => {
            document.getElementById('taskDetailsContent').textContent = JSON.stringify(data, null, 2);
            new bootstrap.Modal(document.getElementById('taskDetailsModal')).show();
        });
}

function retryTask(taskId) {
    if (confirm('确定要重试这个任务吗？')) {
        fetch(`/api/tasks/${taskId}/retry`, { method: 'POST' })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                location.reload();
            });
    }
}

function deleteTask(taskId) {
    currentDeleteTaskId = taskId;
    document.getElementById('deleteTaskId').textContent = taskId;
    new bootstrap.Modal(document.getElementById('deleteConfirmModal')).show();
}

function confirmDeleteTask() {
    if (!currentDeleteTaskId) return;
    
    fetch(`/api/delete_task/${currentDeleteTaskId}`, { method: 'DELETE' })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                // 关闭模态框
                bootstrap.Modal.getInstance(document.getElementById('deleteConfirmModal')).hide();
                // 刷新页面
                location.reload();
            } else {
                alert('删除失败: ' + data.message);
            }
        })
        .catch(error => {
            alert('删除失败: ' + error.message);
        });
}

function reloadConfig() {
    if (confirm('确定要重新加载配置吗？这将应用最新的环境变量设置。')) {
        fetch('/api/reload_config', { method: 'POST' })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('配置重载成功！\n\n当前配置:\n' + 
                          '切片大小: ' + data.config.chunk_size + '\n' +
                          '压缩: ' + (data.config.compression ? '启用' : '禁用') + '\n' +
                          '压缩级别: ' + data.config.compression_level + '\n' +
                          '并行传输: ' + (data.config.parallel ? '启用' : '禁用') + '\n' +
                          '并行数量: ' + data.config.parallel_chunks + '\n' +
                          'HTTP传输: ' + (data.config.http_transfer ? '启用' : '禁用') + '\n' +
                          'rsync: ' + (data.config.rsync ? '启用' : '禁用'));
                } else {
                    alert('配置重载失败: ' + data.message);
                }
            })
            .catch(error => {
                alert('配置重载失败: ' + error.message);
            });
    }
}

function refreshHistory() {
    location.reload();
}
</script>
{% endblock %}
